<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(pid=0,type=1)">添加权限</el-button>
        </div>
        <el-table
          border
          :data="perList"
          row-key="id"
          default-expand-all
        >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="hAdd(row.id,type=2)">添加</el-button>
              <el-button type="text" @click="hEdit(row.id)">编辑</el-button>
              <el-button
                v-if="row.children.length===0"
                type="text"
                @click="hDel(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :visible.sync="dialogShow"
        :title="isEdit?'编辑':'新增'"
        @close="close"
      >
        <el-form ref="formData" label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="dialogShow = false">取消</el-button>
            <el-button type="primary" @click="hSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissions, delPermission, addPermission, getPermissionById, updatePermissionById } from '@/api/permissions'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permissions',
  data() {
    return {
      // 权限列表
      perList: [],
      // 表单数据
      formData: {
        // 当前的id
        pid: 0,
        // 当前的type
        type: 0,
        name: '',
        enVisible: '',
        code: '',
        description: ''
      },
      // 新增弹框显示
      dialogShow: false,
      // 编辑？
      isEdit: false
    }
  },
  created() {
    // 刷新页面
    this.uploadPermissions()
  },
  methods: {
    // 刷新页面
    async uploadPermissions() {
      try {
        const res = await getPermissions()
        // console.log('getPermissions', res)
        this.perList = tranListToTreeData(res.data.data)
        // 保存数据
      } catch (err) {
        console.log('getPermissions', err)
      }
    },
    // 删除页面
    async hDel(id) {
      try {
        await delPermission(id)
        this.$message.success('删除成功')
        this.uploadPermissions()
      } catch (error) {
        console.log(error)
      }
    },
    // 添加按钮，开启弹框
    async hAdd(id, type) {
      this.dialogShow = true
      this.isEdit = false
      this.formData.pid = id
      this.formData.type = type
    },
    // 确定按钮
    hSubmit() {
      this.isEdit ? this.setPermissionById() : this.paddPermission()
    },
    // 弹框关闭
    close() {
      this.formData = {}
      this.$refs.formData.resetFields()
      this.dialogShow = false
    },
    // 编辑框打开
    async  hEdit(id) {
      this.dialogShow = true
      this.isEdit = true
      const res = await getPermissionById(id)
      //   console.log(res)
      this.formData = res.data.data
    },
    // 新增函数
    async paddPermission() {
      try {
        await addPermission(this.formData)
        // console.log(res)
        this.$message.success('新增权限点成功')
        this.dialogShow = false
      } catch (error) {
        console.log(error)
      }
      this.uploadPermissions()
    },
    // 编辑函数
    async setPermissionById() {
      try {
        await updatePermissionById(this.formData)
        // console.log(res)
        this.$message.success('更新权限点成功')
        this.dialogShow = false
      } catch (error) {
        console.log(error)
      }
      this.uploadPermissions()
    }
  }

}
</script>

<style>

</style>
